package com.utils;

import com.config.ProjectConfig;
import com.enums.BizCodeEnum;
import com.exception.BizException;
import com.interceptor.LoginInterceptor;
import com.model.LoginUser;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * 每个用户都只能在自己的namespace下对项目或者文件进行管理，不能越权
 * @author rkc
 * @date 2022/8/8 11:07
 */
@Component
public class FileMappingUtil {

    @Autowired
    private ProjectConfig projectConfig;

    public String mapping(String path) {
        return projectConfig.getStorage() + File.separator + LoginInterceptor.THREAD_LOCAL.get().getUsername() + File.separator + path;
    }

    public String mapping(String token, String path) {
        Claims claims = JwtUtil.checkJWT(token);
        if (claims == null) {
            throw new BizException(BizCodeEnum.ACCOUNT_UN_LOGIN);
        }
        String username = claims.get("username", String.class);
        return projectConfig.getStorage() + File.separator + username + File.separator + path;
    }
}
